/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.annotation.Inner;
import com.nai365.nart.product.entity.Category;
import com.nai365.nart.product.service.CategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.nai365.nart.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品三级分类
 *
 * @author baogs
 * @date 2022-07-01 11:05:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
@Api(value = "category", tags = "商品三级分类管理")
public class CategoryController {

	private final CategoryService categoryService;


	/**
	 * 信息
	 */
	@GetMapping("/info/{catId}")
	public R info(@PathVariable("catId") Long catId) {
		Category category = categoryService.getById(catId);

		return R.ok(category);
	}


	/**
	 * 分页查询
	 *
	 * @param page     分页对象
	 * @param category 商品三级分类
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getCategoryPage(Page page, Category category) {
		return R.ok(categoryService.page(page, Wrappers.query(category)));
	}


	/**
	 * 查询出所有分类以及子分类，以树形结构组装起来列表
	 *
	 * @param type 分页对象
	 * @param type expensiveGoods  catId
	 * @return
	 */
	@ApiOperation(value = "查询出所有分类以及子分类，以树形结构组装起来列表", notes = "查询出所有分类以及子分类，以树形结构组装起来列表")
	@GetMapping("/list/tree")
	public R list(Integer type, String expensiveGoods, Long catId) {

		List<Category> entities = categoryService.listWithTree(type, expensiveGoods, catId);

		return R.ok(entities);
	}

	/**
	 * 通过id查询商品三级分类
	 *
	 * @param catId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{catId}")
	public R getById(@PathVariable("catId") Long catId) {
		return R.ok(categoryService.getById(catId));
	}

	/**
	 * 新增商品三级分类
	 *
	 * @param category 商品三级分类
	 * @return R
	 */
	@ApiOperation(value = "新增商品三级分类", notes = "新增商品三级分类")
	@SysLog("新增商品三级分类")
	@PostMapping
	public R save(@RequestBody Category category) {
		return R.ok(categoryService.save(category));
	}

	/**
	 * 修改商品三级分类
	 *
	 * @param category 商品三级分类
	 * @return R
	 */
	@ApiOperation(value = "修改商品三级分类", notes = "修改商品三级分类")
	@SysLog("修改商品三级分类")
	@PutMapping
	public R updateById(@RequestBody Category category) {
		return R.ok(categoryService.updateById(category));
	}

	/**
	 * 通过id删除商品三级分类
	 *
	 * @param catId id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除商品三级分类", notes = "通过id删除商品三级分类")
	@SysLog("通过id删除商品三级分类")
	@DeleteMapping("/{catId}")
	public R removeById(@PathVariable Long catId) {
		return R.ok(categoryService.removeById(catId));
	}

	/**
	 * 删除
	 *
	 * @RequestBody:获取请求体，必须发送POST请求 SpringMVC自动将请求体的数据(json)转换为对象
	 */
	@DeleteMapping("/delete")
	public R delete(@RequestBody Long[] catIds) {

		//1、检查当前删除的菜单，是否被别的地方引用
		//categoryService.removeByIds(Arrays.asList(catIds));

		categoryService.removeMenuByIds(Arrays.asList(catIds));

		return R.ok();
	}


	/**
	 * 导出excel 表格
	 *
	 * @param category 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<Category> export(Category category) {
		return categoryService.list(Wrappers.query(category));
	}


	@ApiOperation(value = "查询所有分类")
	@GetMapping("/list/all")
	@Inner
	public R<List<Category>> listAll() {
		return R.ok(categoryService.list());
	}
}
